package csu.big_article.controller;

import csu.big_article.pojo.Category;
import csu.big_article.pojo.Result;
import csu.big_article.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //获取用户所有的文章列表
    @GetMapping("/category")
    public Result<List<Category>> getCategory() {
         List<Category> list = categoryService.getCategory();
        return Result.success(list);
    }

    //新增文章分类
    @PostMapping("/addCategory")
    public Result add(@RequestBody @Validated Category category) {
        categoryService.add(category);
        return Result.success();
    }

    //获取文章详细信息
    @GetMapping("/category/detail")
    public Result<Category> getCategoryDetail(@RequestParam Integer id) {
        Category category = categoryService.getCategoryDetail(id);
        return Result.success(category);
    }

    //更新文章分类
    @PutMapping("/category")
    public Result updateCategory(@RequestBody @Validated({Category.Update.class}) Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/category")
    public Result deleteCategory(@RequestParam Integer id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

}
